package com.dzt.cloudreader.fragment;

import android.content.DialogInterface;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.cocosw.bottomsheet.BottomSheet;
import com.dzt.cloudreader.R;
import com.dzt.cloudreader.adapter.AndroidAdapter;
import com.dzt.cloudreader.base.LoadingFragment;
import com.dzt.cloudreader.model.GankIoDataBean;
import com.dzt.cloudreader.network.GankIoDataPresenter;
import com.dzt.cloudreader.network.GankIoDataView;
import com.dzt.cloudreader.utils.JToastKit;
import com.dzt.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by M02323 on 2017/12/15.
 */

public class CustomFragment extends LoadingFragment {
	private static final String GANK_CALA = "gank_cala";
	@BindView(R.id.xrv_custom)
	XRecyclerView recyclerView;
	private AndroidAdapter adapter;
	private List<GankIoDataBean.ResultsBean> lists = new ArrayList<>();
	private GankIoDataPresenter presenter;
	private LayoutInflater inflater;
	private String mType = "all";
	private int mPage = 1;
	private View mHeaderView;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_custom;
	}

	@Override
	protected void initData() {
		inflater = LayoutInflater.from(context);
		adapter = new AndroidAdapter(context, lists, R.layout.item_android);
		initRecyclerView();
		presenter = new GankIoDataPresenter(context);
		presenter.onCreate();
		presenter.attachView(dataView);
		loadCustomData();
	}

	private void initRecyclerView() {
		recyclerView.setPullRefreshEnabled(false);
		recyclerView.clearHeader();
		mHeaderView = inflater.inflate(R.layout.header_item_gank_custom, null, false);
		recyclerView.addHeaderView(mHeaderView);
		initHeader(mHeaderView);
		recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
			@Override
			public void onRefresh() {

			}

			@Override
			public void onLoadMore() {
				mPage++;
				loadCustomData();
			}
		});
		recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		// 需加，不然滑动不流畅
		recyclerView.setNestedScrollingEnabled(false);
		recyclerView.setHasFixedSize(false);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		recyclerView.setAdapter(adapter);
	}

	private void loadCustomData() {
		presenter.getGankIoData(mType, 20, mPage);
	}

	private void initHeader(View mHeaderView) {
		final TextView txName = mHeaderView.findViewById(R.id.tx_name);
		String gankCala = preferenceKit.getStringValue(GANK_CALA, "全部");

		txName.setText(gankCala);
		View view = mHeaderView.findViewById(R.id.ll_choose_catalogue);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View v) {
				new BottomSheet.Builder(getActivity(), R.style.BottomSheet_StyleDialog)
						.title("选择分类")
						.sheet(R.menu.gank_bottomsheet)
						.listener(new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								switch (which) {
									case R.id.gank_all:
										if (isOtherType("全部")) {
											txName.setText("全部");
											mType = "all";// 全部传 all
											mPage = 1;
											adapter.clear();
											preferenceKit.setStringValue(GANK_CALA, "全部");

											showLoading();
											loadCustomData();
										}
										break;
									case R.id.gank_ios:
										if (isOtherType("IOS")) {
											txName.setText("IOS");
											mType = "iOS";// 这里有严格大小写
											mPage = 1;
											adapter.clear();
											preferenceKit.setStringValue(GANK_CALA, "IOS");
											showLoading();
											loadCustomData();
										}
										break;
									case R.id.gank_qian:
										if (isOtherType("前端")) {
											changeContent(txName, "前端");
										}
										break;
									case R.id.gank_app:
										if (isOtherType("App")) {
											changeContent(txName, "App");
										}
										break;
									case R.id.gank_movie:
										if (isOtherType("休息视频")) {
											changeContent(txName, "休息视频");
										}
										break;
									case R.id.gank_resouce:
										if (isOtherType("拓展资源")) {
											changeContent(txName, "拓展资源");
										}
										break;
								}
							}
						}).show();
			}
		});
	}

	private boolean isOtherType(String selectType) {
		String clickText = preferenceKit.getStringValue(GANK_CALA, "全部");
		if (clickText.equals(selectType)) {
			JToastKit.getInstance().showMessage("当前已经是" + selectType + "分类");
			return false;
		} else {
			// 重置XRecyclerView状态，解决 如出现刷新到底无内容再切换其他类别后，无法上拉加载的情况
			recyclerView.reset();
			return true;
		}
	}

	private void changeContent(TextView textView, String content) {
		textView.setText(content);
		mType = content;
		mPage = 1;
		adapter.clear();
		preferenceKit.setStringValue(GANK_CALA, content);
		showLoading();
		loadCustomData();
	}

	@Override
	protected void onRefresh() {
		super.onRefresh();
		mPage = 1;
		adapter.clear();
		loadCustomData();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(presenter != null){
			presenter.onDestroy();
		}
	}

	GankIoDataView dataView = new GankIoDataView() {
		@Override
		public void onSuccess(GankIoDataBean gank) {
			showContentView();
			if (gank != null && gank.getResults() != null && gank.getResults().size() > 0) {
				recyclerView.refreshComplete();
				lists.addAll(gank.getResults());
				adapter.notifyDataSetChanged();
			} else {
				recyclerView.noMoreLoading();
			}
		}

		@Override
		public void onError(String result) {
			recyclerView.refreshComplete();
		}
	};
}
